<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Area extends FOX_Controller {

    function __construct ()
    {
        parent::__construct();
        $this->load->model('areas_m');
        $this->load->model('category_m');
    }
    /**
     * 获取省市区联动
     */
    public function get_area() {
        $p_id = $this->input->get("p_id");

        $data = $this->areas_m->get_sons_by_pid($p_id);
        $html = '<option selected="selected" value="0">可选择</option>';
        if (!empty($data)) {
            foreach ($data as $obj) {
                $html .= '<option value="'.$obj['area_id'].'">'.$obj['area_name'].'</option>';
            }
        }
        echo $html;
    }

    /**
     * 获取经纬度新(因旧接口调取上海经纬度出bug)
     * @author fengchen@usuretech.com
     * @createtime 2016.11.7
     */
    public function get_location(){
        $this->load->library('curl');
        $get = $this->input->get();
        $address = $get["address"];
        $getData['address'] = $address;
        $getData['output'] = 'json';
        $getData['ak'] = '8526cdc9eaa7faa35706d8d0fe54528d';

        $get_url = 'http://api.map.baidu.com/geocoder/v2/?'.http_build_query($getData, NULL, '&');
        $returnStr = $this->curl->simple_get($get_url);
        $returnData = json_decode($returnStr, true);
        if($returnData["result"]["location"]["lat"]!=""){
            $msg = "经纬度获取成功 （地址类型:".$returnData["result"]["level"]."）";
            $arr["lat"]=$returnData["result"]["location"]["lat"];
            $arr["lng"]=$returnData["result"]["location"]["lng"];
            echo json_encode(array('status' => '1', 'msg' => $msg, 'arr' => $arr));
        }else{
            $msg = "经纬度获取失败，请确认你的输入后重新获取!";
            echo json_encode(array('status' => '2', 'msg' => $msg, 'arr' => $returnData));
        }
    }

    /**
     * 获取省市区联动
     */
    public function get_good_category() {
        $web_id = $this->input->get("web_id");
        $good_id = $this->input->get("good_id");
        if($web_id){
            $data = $this->category_m->get_cates_by_all_category($web_id);
        }
        if($good_id){
            $data = $this->category_m->get_sub_cates_by_all_category($good_id);
        }

        if (!empty($data)) {
            $html = '<option selected="selected" value="0">可选择</option>';
            foreach ($data as $obj) {
                $html .= '<option value="'.$obj['id'].'">'.$obj['category_name'].'</option>';
            }
        }else{
            $html  = "0";
        }
        echo $html;
    }

    /**
     * 腾讯位置-地址转经纬度
     */
    public function tencent_location(){
        $this->load->library('curl');
        $get = $this->input->get();
        $address = $get["address"];
        $getData['address'] = $address;
        $getData['output'] = 'json';
        $getData['key'] = '2V3BZ-YYJCF-27BJD-NCI7P-RCT76-75FFR';

        $get_url = 'http://apis.map.qq.com/ws/geocoder/v1/?'.http_build_query($getData, NULL, '&');
        $returnStr = $this->curl->simple_get($get_url);
        $returnData = json_decode($returnStr, true);
        if($returnData["result"]["location"]["lat"]!=""){
            $msg = "经纬度获取成功";
            $arr["lat"]=$returnData["result"]["location"]["lat"];
            $arr["lng"]=$returnData["result"]["location"]["lng"];
            echo json_encode(array('status' => '1', 'msg' => $msg, 'arr' => $arr));
        }else{
            $msg = "经纬度获取失败，请确认你的输入后重新获取!";
            echo json_encode(array('status' => '2', 'msg' => $msg, 'arr' => $returnData));
        }
    }

     /**
     * 清除所有的cookie和session
     */
    public function clean_cs() {
        $this->load->helper('cookie');
        $this->session->unset_userdata("wechat_user");
        $this->session->unset_userdata("target_url");
        $this->session->unset_userdata("wechat_user_info");
        $uid=$this->session->userdata('user_id');
        $this->user_m->del_user_key_by_user_id($uid);
        $this->session->unset_userdata('user_id');
        setcookie('loveme', '', time() - 3600, '/', '', false, true);
        $this->session->sess_destroy();
        delete_cookie("machine_key");
        delete_cookie("qq_city");
        delete_cookie("qq_lat");
        delete_cookie("qq_lng");
        foreach ($_COOKIE as $key => $value) {
            setcookie($key, null);
        }
        pp("清楚缓存成功");exit();
    }
    public  function session(){
        pp($_SESSION);
    }

}
